//
//  YSZMainView.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/23.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit


protocol YSZMainViewDelegate: NSObjectProtocol
{
    func visitorViewRegister()
    
    func visitorViewLogin()
}



class YSZMainView: UIView
{
    
    weak var visitorDelegate: YSZMainViewDelegate?
    
    func visitorViewRegister ()
    {
        visitorDelegate?.visitorViewRegister()
        print(__FUNCTION__)
    }
    func visitorViewLogin()
    {
        visitorDelegate?.visitorViewLogin()
        print(__FUNCTION__)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        prepareUI()
    }
    //设置不同的页面不同的信息
    func setUpInfo(imageName: String, message: String)
    {
        iconView.image = UIImage(named: imageName)
        labelMessage.text = message
        homeView.hidden = true
        labelMessage.sizeToFit()
        sendSubviewToBack(coverView)
        
    }
    //旋转图片
    func startRotationAnimation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        animation.removedOnCompletion=false
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    //暂停旋转
    func pauseAnimation()
    {
        //纪录暂停时间
        let pauseTime=iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        //设置速度为0
        iconView.layer.speed=0
        //设置动画偏移时间
        iconView.layer.timeOffset=pauseTime
    }
    //恢复旋转
    func resumeAnimation()
    {
        //获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        //设置动画速度为1
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        let timeSicePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        iconView.layer.beginTime=timeSicePause
        
    }
    
    private func prepareUI()
    {
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(labelMessage)
        addSubview(registerButton)
        addSubview(logButton)
        //设置自动约束为false
        iconView.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints=false
        labelMessage.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        logButton.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        //添加iconview的约束
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -30))
        //添加homeview的约束
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //添加label的约束
        addConstraint( NSLayoutConstraint(item: labelMessage, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: labelMessage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: labelMessage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //添加registerButton 的约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: labelMessage, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelMessage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //添加logButton的约束
        addConstraint(NSLayoutConstraint(item: logButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: labelMessage, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelMessage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: logButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: logButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //添加遮罩视图约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        
    }
    //轮子
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子2
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //消息
    private lazy var labelMessage: UILabel =
    {
        let lab = UILabel()
        lab.textColor = UIColor.darkGrayColor()
        lab.text = "关注一些人，看看有什么惊喜"
        lab.numberOfLines=0
        lab.sizeToFit()
        return lab
        }()
    //注册按钮
    private lazy var registerButton: UIButton =
    {
        let resbtn = UIButton()
        resbtn.setTitle("注册", forState: UIControlState.Normal)
        resbtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        resbtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        resbtn.sizeToFit()
        
        
        resbtn.addTarget(self, action: "visitorViewRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return resbtn
        }()
    //注册按钮
    private lazy var logButton: UIButton =
    {
        let logbtn = UIButton()
        logbtn.setTitle("登录", forState: UIControlState.Normal)
        logbtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        logbtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        logbtn.sizeToFit()
        logbtn.addTarget(self, action: "visitorViewLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return logbtn
        }()
    //遮罩
    private lazy var coverView : UIImageView =
    {
        let imageName = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: imageName)
            
    }()
    
}
